

#import "CountDownPicker.h"

@implementation CountDownPicker

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
		toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0.0, 0.0, 0.0, 44.0)];
		[toolbar setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
		[toolbar setBackgroundColor: [UIColor clearColor]];

		UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonSystemItemCancel target:self action:@selector(actionCancel:)];
		UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonSystemItemAction target:self action:@selector(actionDone:)];
		
		NSArray *toolItems = [NSArray arrayWithObjects: cancelBtn, spaceItem, doneBtn, nil];
        [spaceItem release];
        [cancelBtn release];
        [doneBtn release];
		[toolbar setItems: toolItems];

		actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
																 delegate:nil
														cancelButtonTitle:nil
												   destructiveButtonTitle:nil
														otherButtonTitles:nil];
		
		[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
		
		picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
		picker.dataSource = self;
		picker.delegate = self;
		picker.showsSelectionIndicator = YES;

		[actionSheet addSubview: toolbar];
		[actionSheet addSubview: picker];
    }
    return self;
}

- (void)dealloc
{
	[actionSheet release]; actionSheet = nil;
    [toolbar release];
    [picker release];
	[super dealloc];
}

- (void)actionCancel:(id)sender
{
	[actionSheet setHidden:YES];
	[actionSheet dismissWithClickedButtonIndex: [actionSheet cancelButtonIndex] animated: YES];
}

- (void)actionDone:(id)sender
{
	BOOL killSelf = YES;
	if (_delegate && [_delegate respondsToSelector: @selector(timeSelected:)]) {
		NSInteger timeDuration = [picker selectedRowInComponent:0]*24*60 + [picker selectedRowInComponent:1]*60 + [picker selectedRowInComponent:2];
		killSelf = [_delegate timeSelected:timeDuration];
	}

	if ( killSelf ) {
		[actionSheet setHidden:YES];
		[actionSheet dismissWithClickedButtonIndex: [actionSheet cancelButtonIndex] animated: YES];
	}
}

- (void)show
{
	UIWindow *wd = [[UIApplication sharedApplication] keyWindow];
	[actionSheet showInView: wd];
	[actionSheet setHidden:NO];
	[actionSheet setBounds: CGRectMake(0, 0, wd.frame.size.width, wd.frame.size.height)];
}

- (void)showWithTime:(NSInteger)minutes
{
    NSInteger hourBefore = minutes/60;
    NSInteger dayBefore = hourBefore/24;
    if (dayBefore > 0) {
        if (dayBefore > 30) dayBefore = 30;
        [picker selectRow:dayBefore inComponent:0 animated:NO];
        [picker selectRow:hourBefore%24 inComponent:1 animated:NO];
        [picker selectRow: minutes%60 inComponent:2 animated:NO];
    } else if (hourBefore > 0) {
        [picker selectRow:0 inComponent:0 animated:NO];
        [picker selectRow:hourBefore inComponent:1 animated:NO];
        [picker selectRow: minutes%60 inComponent:2 animated:NO];
    } else if (minutes > 0) {
        [picker selectRow:0 inComponent:0 animated:NO];
        [picker selectRow:0 inComponent:1 animated:NO];
        [picker selectRow:minutes inComponent:2 animated:NO];
    }

    [self show];
}


#pragma mark UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == COUNT_PICKER_COMPONENT_DAY) {
		return [NSString stringWithFormat:@"%d天", row];
    } else if (component == COUNT_PICKER_COMPONENT_HOUR) {
		return [NSString stringWithFormat:@"%d小时", row];
    } else if (component == COUNT_PICKER_COMPONENT_MINUTE) {
		return [NSString stringWithFormat:@"%d分钟", row];
    }
    return nil;
}


#pragma mark UIPickerViewDataSource

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
	UILabel *vLabel = (UILabel *)view;
	if (!vLabel) {
		vLabel = [[[UILabel alloc] initWithFrame: CGRectZero] autorelease];
		[vLabel setNumberOfLines: 2];
		[vLabel setLineBreakMode: UILineBreakModeCharacterWrap];
		[vLabel setTextAlignment: UITextAlignmentCenter];
		[vLabel setBackgroundColor: [UIColor clearColor]];
	}

	CGSize rowSize = [pickerView rowSizeForComponent: component];
	CGRect rect = CGRectMake(3.0, 0.0, rowSize.width - 6.0, rowSize.height);
	[vLabel setFrame: rect];

	NSString *str = [self pickerView: pickerView titleForRow: row forComponent: component];
	[vLabel setText: str];

	// 估算字体
	UIFont *font = [UIFont systemFontOfSize: 15.0];
	CGSize nSize = [str sizeWithFont: font constrainedToSize: CGSizeMake(rect.size.width, 200.0) lineBreakMode: UILineBreakModeCharacterWrap];
	if (nSize.height >= 39.0) { // > 38.0
		[vLabel setFont: [UIFont systemFontOfSize: 11.0]];
	} else if (nSize.height >= 20.0) { // > 19.0
		[vLabel setFont: [UIFont systemFontOfSize: 13.0]];
	} else {
		[vLabel setFont: font];
	}

    return vLabel;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return COUNT_PICKER_COMPONENT_COUNT;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == COUNT_PICKER_COMPONENT_DAY) {
        return 31;
    } else if (component == COUNT_PICKER_COMPONENT_HOUR) {
        return 24;
    } else if (component == COUNT_PICKER_COMPONENT_MINUTE) {
        return 60;
    }
    return 0;
	
}

@end
